﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ExtBusiness;

/// <summary>
/// PageBase 的摘要说明
/// </summary>
public class PageBase : System.Web.UI.Page
{
    /// <summary>
    /// checkLogin = 1表示要判断是否登陆
    /// </summary>
    private int checkLogin = 1;

    public PageBase()
    {
    }


    /// <summary>
    /// 设置页面的主题
    /// </summary>
    /// <param name="e"></param>
    protected override void OnPreInit(EventArgs e)
    {
        base.OnPreInit(e);
    }


    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
      //  没有登陆或登陆已经过期
        if (checkLogin == 1)
        {
            if (!String.IsNullOrEmpty(Request["SId"]))//填表控件调用的。
            {
                Session["UserId"] = ExtBusiness.Admin.User.GetUserLoginSession(Request["SID"]);
            }

            if (Session["UserId"] == null)
            {
                if ((Session["UserName"] == null || Session["UserName"].ToString() == String.Empty)
                     && Request.Url.ToString().ToLower().IndexOf(FormsAuthentication.LoginUrl.ToLower()) < 0)   
                {
                    Response.Redirect("http://" + Request.ServerVariables["HTTP_HOST"] + FormsAuthentication.LoginUrl);    
                }
            }
        }
    }

    #region 公有属性
    /// <summary>
    /// 用户名称，存贮在Session中
    /// </summary>
    protected string UserName
    {
        get
        {
            if (Session["UserName"] != null)
            {
                return Session["UserName"].ToString();
            }
            else
            {
                return string.Empty;
            }
        }
    }
    /// <summary>
    /// 用户名称（ＩＤ），存贮在Session中
    /// </summary>
    protected string UserId
    {
        get
        {
            if (Session["UserId"] != null)
            {
                return Session["UserId"].ToString();
            }
            else
            {
                return string.Empty;
            }
        }
    }
    /// <summary>
    /// 部门ＩＤ，存贮在Session中
    /// </summary>
    protected string GroupId
    {
        get
        {
            if (Session["GroupId"] != null)
            {
                return Session["GroupId"].ToString();
            }
            else
            {
                return string.Empty;
            }
        }
    }

    /// <summary>
    /// 部门ＩＤ，存贮在Session中
    /// </summary>
    protected string RealUserId
    {
        get
        {
            if (Session["RealUserId"] != null)
            {
                return Session["RealUserId"].ToString().ToUpper();
            }
            else
            {
                return string.Empty;
            }
        }
    }

    /// <summary>
    /// 用户密码
    /// </summary>
    protected string Password
    {
        get
        {
            if (Session["Password"] != null)
            {
                return Session["Password"].ToString();
            }
            else
            {
                return string.Empty;
            }
        }
    }


    #endregion


    #region 客户端IP
    /// <summary>
    /// 客户端IP 
    /// </summary>
    protected string HostIP
    {
        get
        {
            if (Request.PathInfo != null)
            {
                return Page.Request.UserHostAddress.ToString();
            }
            else
            {
                return string.Empty;
            }

        }

    }
    #endregion

    /// <summary>
    /// 判断是否启用登陆判断
    /// </summary>
    public int CheckLogin
    {
        set
        {
            checkLogin = value;
        }
        get
        {
            return checkLogin;
        }
    }
}
